package org.cytoscape.myapp.cytoscape_app_v1.internal.tasks.groups;

import java.util.Set;
import java.util.Vector;

import org.cytoscape.application.CyApplicationManager;
import org.cytoscape.group.CyGroupFactory;
import org.cytoscape.group.CyGroupManager;
import org.cytoscape.myapp.cytoscape_app_v1.internal.grupos.FactoryInterface;
import org.cytoscape.myapp.cytoscape_app_v1.internal.grupos.FactorySensitive;
import org.cytoscape.myapp.cytoscape_app_v1.internal.grupos.Grupos;
import org.cytoscape.myapp.cytoscape_app_v1.internal.grupos.clasificadores.Clasificador;
import org.cytoscape.myapp.cytoscape_app_v1.internal.grupos.clasificadores.ClasificadorXor;
import org.cytoscape.myapp.cytoscape_app_v1.internal.grupos.comparadores.Comparador;
import org.cytoscape.myapp.cytoscape_app_v1.internal.grupos.comparadores.ComparadorIgual;
import org.cytoscape.myapp.cytoscape_app_v1.internal.grupos.criterios.Criterio;
import org.cytoscape.myapp.cytoscape_app_v1.internal.grupos.criterios.CriterioVariable;
import org.cytoscape.myapp.cytoscape_app_v1.internal.helpers.ModelUtils;
import org.cytoscape.myapp.cytoscape_app_v1.internal.tasks.GeneradorGrupos;
import org.cytoscape.work.FinishStatus;
import org.cytoscape.work.ObservableTask;
import org.cytoscape.work.Task;
import org.cytoscape.work.TaskIterator;
import org.cytoscape.work.TaskManager;
import org.cytoscape.work.TaskMonitor;
import org.cytoscape.work.TaskObserver;

public class GroupsCreationTask implements Task {
	private CyApplicationManager applicationManager;
	private CyGroupManager groupManager;
	private CyGroupFactory groupFactory;
	private String attr;
	@SuppressWarnings("rawtypes")

	
	public GroupsCreationTask(CyApplicationManager applicationManager, CyGroupManager groupManager, CyGroupFactory groupFactory,
								String attr){
		this.applicationManager = applicationManager;
		this.groupManager = groupManager;
		this.groupFactory = groupFactory;
		this.attr = attr;
		
	}
	
	
	@Override
	public void run(TaskMonitor taskMonitor) throws Exception {
		
		
		taskMonitor.setTitle("Generando Grupos ");
		taskMonitor.setStatusMessage("Iniciando");
		taskMonitor.setStatusMessage("Procensando Nodos");
		
		Vector<Criterio> criterios = new Vector<Criterio>();
		FactoryInterface factSense = new FactorySensitive(); 

		Set<Object> attrs = ModelUtils.getValuesCyColumnNetwork(applicationManager.getCurrentNetwork(),attr);
		for(Object elem:attrs){
			Comparador or= new ComparadorIgual(elem);
			Criterio crit = new CriterioVariable(or, elem.toString(), factSense);
			criterios.add(crit);
		}
		Clasificador clasificador = new ClasificadorXor(criterios);
		GeneradorGrupos generador = new GeneradorGrupos(applicationManager.getCurrentNetwork(), clasificador,
											new Grupos(groupManager, groupFactory, applicationManager.getCurrentNetwork()), 
													ModelUtils.getAllNodesWhitAttributesSimple(applicationManager.getCurrentNetwork(), attr));
		generador.generarGrupos();
		taskMonitor.setStatusMessage("Finalizando");
	}

	@Override
	public void cancel() {
		// TODO Auto-generated method stub
		
	}

	

}
